package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.utils.CharsetUtil;
import com.cz.nongyetongv5.utils.ConfigUtil;
import com.cz.nongyetongv5.utils.GoHomeUtil;

public class SanPinChaXunActivity extends Activity implements OnClickListener {

	private ViewGroup btnBack = null;

	// /**
	// * 绿色系列
	// */
	// private ViewGroup btnLvSeSearch = null;

	// private EditText edtLvSeFilter = null;

	/**
	 * 无公害系列
	 */
	private ViewGroup btnWghSearch = null;

	private EditText edtWghFilter = null;

	/**
	 * 企业名称系列
	 */
	private ViewGroup btnQySearch = null;

	private EditText edtQyFilter = null;

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);

		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.sanpin_chaxun);

		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		// btnLvSeSearch = (ViewGroup) findViewById(R.id.btnLvSeSearch);
		//
		// btnLvSeSearch.setOnClickListener(this);

		// edtLvSeFilter = (EditText) findViewById(R.id.edtLvSeFilter);

		btnWghSearch = (ViewGroup) findViewById(R.id.btnWghSearch);
		btnWghSearch.setOnClickListener(this);

		edtWghFilter = (EditText) findViewById(R.id.edtWghFilter);

		btnQySearch = (ViewGroup) findViewById(R.id.btnQySearch);
		btnQySearch.setOnClickListener(this);

		edtQyFilter = (EditText) findViewById(R.id.edtQyFilter);

		loadRightTitle();

	}

	/**
	 * 加载去主页的按钮
	 */
	public void loadRightTitle() {

		ViewGroup btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		View view = GoHomeUtil.createPopView(getApplicationContext(),
				new String[] { "主页" });

		// 弹出更多的菜单
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);

		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());

		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);

		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);

		final Button btn01 = (Button) pop.getContentView().findViewWithTag(
				"btn0");

		OnClickListener menuOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (v == btn01) {
					GoHomeUtil.goHome(getApplicationContext());
				}
			}
		};

		btn01.setOnClickListener(menuOnClick);

		btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		btnMeanMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pop.isShowing()) {
					// 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
					pop.dismiss();
				} else {
					// 显示窗口
					pop.showAsDropDown(v);
				}

			}
		});

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {

		if (v == btnBack) {
			finish();
		}

		// if (v == btnLvSeSearch) {
		//
		// String hint = edtLvSeFilter.getHint().toString();
		//
		// String text = edtLvSeFilter.getText().toString();
		//
		// String url = CommonConstants.URLSUFFER
		// + getString(R.string.sanpin_lvse_url);
		//
		// String title = "三品查询";
		//
		// if (CharsetUtil.isNotNull(text)) {
		//
		// Intent intent = new Intent(SanPinChaXunActivity.this,
		// SanPinChaXunDetailActivity.class);
		//
		// intent.putExtra("hint", hint);
		// intent.putExtra("text", text);
		// intent.putExtra("url", url);
		// intent.putExtra("title", title);
		//
		// startActivity(intent);
		//
		// } else {
		// Toast.makeText(SanPinChaXunActivity.this, "尚未输入查询条件",
		// Toast.LENGTH_SHORT).show();
		// }
		//
		// }

		if (v == btnWghSearch) {

			String hint = edtWghFilter.getHint().toString();

			String text = edtWghFilter.getText().toString();

			String url = new ConfigUtil(getResources()).getPreHttpUrl()
					+ getString(R.string.sanpin_wgh_url);

			String title = "按证书编号查询";

			if (CharsetUtil.isNotNull(text)) {

				Intent intent = new Intent(SanPinChaXunActivity.this,
						SanPinChaXunDetailActivity.class);

				intent.putExtra("hint", hint);
				intent.putExtra("text", text);
				intent.putExtra("url", url);
				intent.putExtra("title", title);

				startActivity(intent);

			} else {
				Toast.makeText(SanPinChaXunActivity.this, "尚未输入查询条件",
						Toast.LENGTH_SHORT).show();
			}

		}

		if (v == btnQySearch) {

			String hint = edtQyFilter.getHint().toString();

			String text = edtQyFilter.getText().toString();

			String url = new ConfigUtil(getResources()).getPreHttpUrl()
					+ getString(R.string.sanpin_qy_url);

			String title = "按企业名称查询";

			if (CharsetUtil.isNotNull(text)) {

				Intent intent = new Intent(SanPinChaXunActivity.this,
						SanPinChaXunDetailActivity.class);

				intent.putExtra("hint", hint);
				intent.putExtra("text", text);
				intent.putExtra("url", url);
				intent.putExtra("title", title);
				startActivity(intent);

			} else {
				Toast.makeText(SanPinChaXunActivity.this, "尚未输入查询条件",
						Toast.LENGTH_SHORT).show();
			}

		}

	}

}
